package com.classic.okhttp.gateway.http;

import android.app.Activity;
import android.app.Service;
import android.text.TextUtils;

import com.classic.okhttp.base.OkHttpUtils;
import com.classic.okhttp.base.callback.Callback;

import java.util.HashMap;
import java.util.Map;

/**
 * 使用步骤： <br />
 * 1.在application初始化: <br />
 * ActionHelp.init(imei,psdnIp, true); <br />
 *
 * @author Jayden
 */
public class ActionHelp {

    private static final String URL_R = "http://banxiaobao.com:8015/app/"; //商用
    private static final String URL_T = "http://testglorymi.com:8015/app/"; //商测
    //    private static final String URL_T = "http://192.168.1.42:8015/gbb/app/"; //商测
    public static final String CONTENTTYPE = "application/xml;charset=UTF-8";
    public static final String UPLOAD_CONTENTTYPE = "application/octet-stream";
    private static final String CHARTSET = "UTF-8";
    private static String ACCOUNTTYPE = "1";                                      //1手机账号  2 邮箱
    private static String CLIENTTYPE = "android_1.0.0";
    private static String CLIENT_IMEI;
    private static String USER_IP;
    private static String ACCOUNT;
    private static String URL;
    private static boolean ISRELEASE;

    /**
     * 接口调用前，需要调用此方法
     *
     * @param imei
     * @param psdnIp
     * @param isRelease 配置服务环境 true:商用，false:商测
     */
    public static void init(String imei, String psdnIp, boolean isRelease) {
        CLIENT_IMEI = TextUtils.isEmpty(imei) ? "" : imei;
        USER_IP = TextUtils.isEmpty(psdnIp) ? "" : psdnIp;
        ISRELEASE = isRelease;
        URL = isRelease ? URL_R : URL_T;
    }


    /**
     * 获取帐号类型 1：手机账号, 2: 邮箱
     */
    public static String getAccountType() {
        return ACCOUNTTYPE;
    }

    /**
     * 设置账号类型 1：手机账号,2: 邮箱
     */
    public static void setAccountType(String accountType) {
        ACCOUNTTYPE = accountType;
    }

    /**
     * 获取客户端类型
     */
    public static String getClientType() {
        return CLIENTTYPE;
    }

    /**
     * 设置客户端类型
     */
    public static void setClientType(String clientType) {
        CLIENTTYPE = clientType;
    }

    /**
     * 获取当前帐号
     */
    public static String getAccount() {
        return ACCOUNT;
    }

    /**
     * 设置账号
     */
    public static void setAccount(String account) {
        ACCOUNT = account;
    }

    /**
     * 获取接口url
     *
     * @return
     */
    public static String getUrl() {
        return URL;
    }

    /**
     * 获取登录key
     */
//    public static String getLoginKey() {
//        return LOGIN_KEY;
//    }

    /**
     * 设置登录key(登录成功时调用)
     *
     * @param key
     */
//    public static void setLoginKey(String key) {
//        if (!TextUtils.isEmpty(key)) LOGIN_KEY = key;
//    }

    /**
     * 设置默认的登录key(切换账号、注销时调用)
     */
//    public static void setDefaultLoginKey() {
//        LOGIN_KEY = "LQSW@)!$";
//    }


    /**
     * 上传数据
     *
     * @param activity
     * @param longitude
     * @param latitude
     * @param recivedata
     * @param type
     * @param callback
     */
    public static void gbGetCarData(Activity activity, double longitude, double latitude, String recivedata, String type, Callback callback) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("type", type);
        params.put("recivedata", recivedata);
        params.put("longitude", longitude + "");
        params.put("latitude", latitude + "");

        OkHttpUtils.post()
                .url(getUrl("addvehicle.do?"))
                .tag(activity)
                .params(params)
                .build()
                .execute(callback);
    }

    /**
     * 上传刷卡数据
     *
     * @param activity
     * @param punchCardRecord
     * @param callback
     */
    public static void gbUploadCardData(Activity activity, String punchCardRecord, Callback callback) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("punchCardRecord", punchCardRecord);

        OkHttpUtils.post()
                .url(getUrl("payByCard.do?"))
                .tag(activity)
                .params(params)
                .build()
                .execute(callback);
    }


    /**
     * 获取学生信息
     *
     * @param activity
     * @param deviceAddress
     * @param callback
     */
    public static void gbGetAllStudents(Activity activity, String deviceAddress, String timestamp, Callback callback) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("deviceAddress", deviceAddress);
        params.put("timestamp", timestamp);

        OkHttpUtils.post()
                .url(getUrl("queryChildrenAll.do?"))
                .tag(activity)
                .params(params)
                .build()
                .execute(callback);
    }


    /**
     * 上传体温
     *
     * @param activity
     * @param cardNo
     * @param temperature
     * @param callback
     */
    public static void gbUploadTemperature(Activity activity, String cardNo, String temperature, Callback callback) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("temperature", temperature);
        params.put("cardNo", cardNo);

        OkHttpUtils.post()
                .url(getUrl("temperature.do?"))
                .tag(activity)
                .params(params)
                .build()
                .execute(callback);
    }


    /**
     * 检查版本
     *
     * @param activity
     * @param callback
     */
    public static void gbVersion(Activity activity, Callback callback) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("type", "3");
        OkHttpUtils.post()
                .url(getUrl("queryNewVersion.do?"))
                .params(params)
                .tag(activity)
                .build()
                .execute(callback);
    }

    private static String getUrl(String action) {
        return new StringBuffer(URL).append(action).toString();
    }


    private static Map<String, String> getHeaders() {
        final HashMap<String, String> headerMap = new HashMap<>();
        headerMap.put("Connection", "Keep-Alive");
        headerMap.put("Chartset", CHARTSET);
        headerMap.put("Cache-Control", "no-cache");
        headerMap.put("Accept-Language", "zh-CN,en-US");
        headerMap.put("client-imei", CLIENT_IMEI);
        headerMap.put("user-ip", USER_IP);
        if (!TextUtils.isEmpty(ACCOUNT)) {
            headerMap.put("phone-number", ACCOUNT);
        }
        return headerMap;
    }

    private static Map<String, String> getHeaders(String account) {
        final HashMap<String, String> headerMap = new HashMap<>();
        headerMap.put("Connection", "Keep-Alive");
        headerMap.put("Chartset", CHARTSET);
        headerMap.put("Cache-Control", "no-cache");
        headerMap.put("Accept-Language", "zh-CN,en-US");
        headerMap.put("client-imei", CLIENT_IMEI);
        headerMap.put("user-ip", USER_IP);
        if (!TextUtils.isEmpty(account)) {
            headerMap.put("phone-number", account);
        }
        return headerMap;
    }
}
